class Solution
{
public:
    int minTrioDegree(int n, vector<vector<int>> &edges)
    {
        vector<vector<int>> adjMat(n + 1, vector<int>(n + 1, 0));
        vector<int> inDegree(n + 1, 0);
        for (auto &edge : edges)
        {
            adjMat[edge[0]][edge[1]] = 1;
            adjMat[edge[1]][edge[0]] = 1;
            ++inDegree[edge[0]];
            ++inDegree[edge[1]];
        }

        int minDeg = INT_MAX;
        for (int i = 1; i <= n; ++i)
        {
            for (int j = i + 1; j <= n; ++j)
            {
                if (!adjMat[i][j])
                {
                    continue;
                }
                for (int k = j + 1; k <= n; ++k)
                {
                    if (!(adjMat[i][k] && adjMat[j][k]))
                    {
                        continue;
                    }
                    minDeg = min(minDeg, inDegree[i] + inDegree[j] + inDegree[k] - 6);
                }
            }
        }
        return minDeg == INT_MAX ? -1 : minDeg;
    }
};